package com.chris.libs.devtools.x.v1.utils;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.widget.ImageView;

import com.linchaolong.android.imagepicker.ImagePicker;
import com.linchaolong.android.imagepicker.cropper.CropImage;
import com.linchaolong.android.imagepicker.cropper.CropImageView;

/**
 * Created by Chris Chan
 * 2020/5/3 8:42
 * Use for:
 * Explain:
 */
public class ImagePickerUtil {
    private ImagePicker imagePicker;
    private ImagePicker.Callback callback;
    private Activity activity;
    private ImageView imageView;
    private int reqWidth = 96, reqHeight = 96, aspectRatioX = 1, aspectRatioY = 1;
    private String title;
    private boolean isCropImage;

    private ImagePickerUtil(Builder builder) {
        this.activity = builder.activity;
        this.imagePicker = builder.imagePicker;
        this.callback = builder.callback;
        this.imageView = builder.imageView;
        this.reqWidth = builder.reqWidth;
        this.reqHeight = builder.reqHeight;
        this.aspectRatioX = builder.aspectRatioX;
        this.aspectRatioY = builder.aspectRatioY;
        this.title = builder.title;
        this.isCropImage = builder.isCropImage;
    }

    public ImagePicker getImagePicker() {
        return imagePicker;
    }

    public void startChooser() {
        this.imagePicker.startChooser(this.activity, this.callback);
    }

    public void startCamera() {
        this.imagePicker.startCamera(this.activity, this.callback);
    }

    public void startGallery() {
        this.imagePicker.startGallery(this.activity, this.callback);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        this.imagePicker.onActivityResult(this.activity, requestCode, resultCode, data);
    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        this.imagePicker.onRequestPermissionsResult(this.activity, requestCode, permissions, grantResults);
    }

    /**
     * 判断是否图片拾取裁剪等操作的返回
     * 原来的封装固定了一些请求码，但是没有预留用于区别的自定义请求码
     *
     * @param requestCode
     * @return
     */
    public static boolean isImagePicked(int requestCode) {
        if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE ||
                requestCode == CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE ||
                requestCode == CropImage.CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE ||
                requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE ||
                requestCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            return true;
        }
        return false;
    }

    public static Builder builder() {
        return new Builder();
    }

    /**
     * 简洁清晰的创建方式
     *
     * @param tag
     * @param title
     * @param activity
     * @param isCorpImage
     * @param reqWidth
     * @param reqHeight
     * @param aspectRatioX
     * @param aspectRatioY
     * @param onImagePickedCompleteListener
     * @return
     */
    public static ImagePickerUtil create(String tag, String title, Activity activity, boolean isCorpImage, int reqWidth, int reqHeight, int aspectRatioX, int aspectRatioY, OnImagePickedCompleteListener onImagePickedCompleteListener) {
        return builder()
                .tag(tag)
                .title(title)
                .activity(activity)
                .cropImage(isCorpImage)
                .reqWidthHeight(reqWidth, reqHeight)
                .aspectRatioXY(aspectRatioX, aspectRatioY)
                .listener(onImagePickedCompleteListener)
                .build();
    }

    public static final class Builder {
        private String tag = "TAG";
        private Activity activity;
        private ImagePicker imagePicker;
        private ImagePicker.Callback callback;
        private OnImagePickedCompleteListener onImagePickedCompleteListener;
        private ImageView imageView;
        private int reqWidth = 96;
        private int reqHeight = 96;
        private int aspectRatioX = 1;
        private int aspectRatioY = 1;
        private String title;
        private boolean isCropImage;

        private Builder() {
        }

        public ImagePickerUtil build() {
            this.imagePicker = new ImagePicker();
            // 设置标题
            this.imagePicker.setTitle(title);
            // 设置是否裁剪图片
            this.imagePicker.setCropImage(isCropImage);
            if (this.callback == null) {
                this.callback = new ImagePicker.Callback() {
                    // 选择图片回调
                    @Override
                    public void onPickImage(Uri imageUri) {
                        if (null != onImagePickedCompleteListener) {
                            onImagePickedCompleteListener.onPickImage(tag, imageUri);
                        }
                    }

                    // 裁剪图片回调
                    @Override
                    public void onCropImage(Uri imageUri) {
                        if (null != onImagePickedCompleteListener) {
                            onImagePickedCompleteListener.onCropImage(tag, imageUri);
                        } else {
                            if (null != imageView) {
                                imageView.setImageURI(imageUri);
                                imageView.setTag(imageUri.getPath());
                            }
                        }
                    }

                    // 自定义裁剪配置
                    @Override
                    public void cropConfig(CropImage.ActivityBuilder builder) {
                        builder
                                // 是否启动多点触摸
                                .setMultiTouchEnabled(false)
                                // 设置网格显示模式
                                .setGuidelines(CropImageView.Guidelines.OFF)
                                // 圆形/矩形
                                .setCropShape(CropImageView.CropShape.RECTANGLE)
                                // 调整裁剪后的图片最终大小（单位：px）
                                .setRequestedSize(reqWidth, reqHeight)
                                // 裁剪框宽高比
                                .setAspectRatio(aspectRatioX, aspectRatioY);
                    }

                    // 用户拒绝授权回调
                    @Override
                    public void onPermissionDenied(int requestCode, String[] permissions,
                                                   int[] grantResults) {
                    }
                };
            }
            //参数齐备之后创建
            return new ImagePickerUtil(this);
        }

        public Builder tag(String tag) {
            this.tag = tag;
            return this;
        }

        public Builder activity(Activity activity) {
            this.activity = activity;
            return this;
        }

        public Builder callback(ImagePicker.Callback callback) {
            this.callback = callback;
            return this;
        }

        public Builder listener(OnImagePickedCompleteListener onImagePickedCompleteListener) {
            this.onImagePickedCompleteListener = onImagePickedCompleteListener;
            return this;
        }

        public Builder imageView(ImageView imageView) {
            this.imageView = imageView;
            return this;
        }

        @Deprecated
        public Builder reqWidth(int reqWidth) {
            this.reqWidth = reqWidth;
            return this;
        }

        @Deprecated
        public Builder reqHeight(int reqHeight) {
            this.reqHeight = reqHeight;
            return this;
        }

        public Builder reqWidthHeight(int reqWidth, int reqHeight) {
            this.reqWidth = reqWidth;
            this.reqHeight = reqHeight;
            return this;
        }

        @Deprecated
        public Builder aspectRatioX(int aspectRatioX) {
            this.aspectRatioX = aspectRatioX;
            return this;
        }

        @Deprecated
        public Builder aspectRatioY(int aspectRatioY) {
            this.aspectRatioY = aspectRatioY;
            return this;
        }

        public Builder aspectRatioXY(int aspectRatioX, int aspectRatioY) {
            this.aspectRatioX = aspectRatioX;
            this.aspectRatioY = aspectRatioY;
            return this;
        }

        public Builder title(String title) {
            this.title = title;
            return this;
        }

        public Builder cropImage(boolean cropImage) {
            isCropImage = cropImage;
            return this;
        }
    }

    public static interface OnImagePickedCompleteListener {
        /**
         * 图片选择回调
         *
         * @param tag
         * @param imageUri
         */
        default void onPickImage(String tag, Uri imageUri) {
        }

        /**
         * 图片裁剪回调
         *
         * @param tag
         * @param imageUri
         */
        default void onCropImage(String tag, Uri imageUri) {
        }
    }
}
